<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            @keyup.enter.native="getGoodsList"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="id"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          width="88px"
          prop="goods_weight"
        ></el-table-column>
        <el-table-column label="创建时间" width="150px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 8, 13, 21, 34]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 8,
      },
      //商品列表
      goodsList: [],
      //总数据条数
      total: 0,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //分页获取商品列表
    async getGoodsList() {
      const { data: response } = await this.$http.get('goods', {
        params: this.queryInfo,
      });
      if (response.meta.status !== 200)
        return this.$message({
          message: response.meta.msg,
          type: 'error',
          center: true,
        });
      this.goodsList = response.data.goods;
      this.total = response.data.total;
    },
    //当选择每页展示多少条数据触发
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    //当页码值被点击被改变执行
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    //删除商品
    async removeById(id) {
      const confirmResult = await this.$confirm('确认永久删除该商品?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err);
      if (confirmResult !== 'confirm') return;
      const { data: response } = await this.$http.delete('goods/' + id);
      if (response.meta.status !== 200)
        return this.$message({
          message: response.meta.msg,
          type: 'error',
          center: true,
        });
      this.$message({
        message: response.meta.msg,
        type: 'success',
        center: true,
      });
      this.getGoodsList();
    },
    //当点击添加商品通过编程式导航跳转到商品添加页面
    goAddPage(){
      this.$router.push('/goods/add')
    }
  },
};
</script>

<style scoped>
</style>